function initCustomTimePicker(_customTimeSetting) {
    var _timeArray = ruleToDaytime(_customTimeSetting);

    console.log(_timeArray);
    var _cuatomTimePre = _customTimeSetting.customTimePre;
    var _customTimeMax = _customTimeSetting.customTimeMax;

    var _startTime = new Date();

    if (_cuatomTimePre.indexOf('D') >= 0) {
        _startTime.setDate(_startTime.getDate() + parseInt(_cuatomTimePre.replace('D', '')));
    } else if (_cuatomTimePre.indexOf('H') >= 0) {
        _startTime.setHours(_startTime.getHours() + parseInt(_cuatomTimePre.replace('H', '')));
    } else if (_cuatomTimePre.indexOf('M') >= 0) {
        _startTime.setMinutes(_startTime.getMinutes() + parseInt(_cuatomTimePre.replace('M', '')));
    }

    var _endTime = new Date();

    _endTime.setDate(_endTime.getDate() + parseInt(_customTimeMax));

    return generateCustomTimeDataRough(_startTime, _endTime, _timeArray);
} //将自提时间的规则解析成周一到周日每天可自提的时间段

function ruleToDaytime(_customTimeSetting) {
    var _customTimeSize = _customTimeSetting.customTimeSize;

    var _customTimeArray = JSON.parse(_customTimeSetting.customTime);

    var _timeArray = [];

    for (var _i = 0; _i < _customTimeArray.length; _i++) {
        var _customTime = _customTimeArray[_i];
        var _startHour = _customTime.startHour;
        var _startMinute = _customTime.startMinute;
        var _endHour = _customTime.endHour;
        var _endMinute = _customTime.endMinute;
        var _everydayTimeSpaceArray = [];
        var _timeItem = {};

        if ('0' == _customTimeSize) {
            //时段细分-天
            var _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);

            _timeItem['id'] = '1';
            _timeItem['value'] = _everydayTimeSpace;

            _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
        } else if ('1' == _customTimeSize) {
            //时段细分-上午下午晚上
            var _everydayTimeSpace = '';

            if (compareTime(_startHour, _startMinute, 12, 0) == -1) {
                //开门时间小于12:00
                if (compareTime(_endHour, _endMinute, 12, 0) != 1) {
                    //关门时间<=12:00
                    _everydayTimeSpace =
                        PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                } else if (compareTime(_endHour, _endMinute, 12, 0) == 1 && compareTime(_endHour, _endMinute, 18, 0) != 1) {
                    //关门时间>12:00且<=18:00
                    _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ 12:00';
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _everydayTimeSpace = '12:00 ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '2';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                } else {
                    //关门时间>18:00
                    _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ 12:00';
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _everydayTimeSpace = '12:00 ~ 18:00';
                    _timeItem['id'] = '2';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _everydayTimeSpace = '18:00 ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '3';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }
            } else if (compareTime(_startHour, _startMinute, 12, 0) != -1 && compareTime(_startHour, _startMinute, 18, 0) == -1) {
                //开门时间>=12：00且<18：00
                if (compareTime(_endHour, _endMinute, 12, 0) == 1 && compareTime(_endHour, _endMinute, 18, 0) != 1) {
                    //关门时间>12:00且<=18:00
                    _everydayTimeSpace =
                        PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                } else {
                    //关门时间>18:00
                    _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ 18:00';
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _everydayTimeSpace = '18:00 ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '2';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }
            } else {
                //开门时间>=18:00
                _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                _timeItem['id'] = '1';
                _timeItem['value'] = _everydayTimeSpace;

                _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
            }
        } else if ('2' == _customTimeSize) {
            //时段细分-小时
            var _everydayTimeSpace = '';

            if (parseInt(_startHour) == parseInt(_endHour)) {
                _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                _timeItem['id'] = '1';
                _timeItem['value'] = _everydayTimeSpace;

                _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
            } else {
                var _hour = parseInt(_startHour) + 1;

                _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_hour, 2) + ':00';
                _timeItem['id'] = '1';
                _timeItem['value'] = _everydayTimeSpace;

                _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                for (; _hour < parseInt(_endHour); _hour++) {
                    _everydayTimeSpace = PrefixInteger(_hour, 2) + ':00 ~ ' + PrefixInteger(_hour + 1, 2) + ':00';

                    var _id = parseInt(_timeItem['id']) + 1;

                    _timeItem['id'] = _id + '';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }

                if (parseInt(_endMinute) != 0) {
                    _everydayTimeSpace = PrefixInteger(_hour, 2) + ':00 ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);

                    var _id = parseInt(_timeItem['id']) + 1;

                    _timeItem['id'] = _id + '';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }
            }
        } else if ('3' == _customTimeSize) {
            //时段细分-半小时
            var _hour;

            var _minute;

            if (parseInt(_startMinute) < 30) {
                _hour = parseInt(_startHour);
                _minute = 30;

                if (compareTime(_startHour, 30, _endHour, _endMinute) != -1) {
                    _everydayTimeSpace =
                        PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                } else {
                    _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_startHour, 2) + ':30';
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }
            } else {
                _hour = parseInt(_startHour) + 1;
                _minute = 0;

                if (compareTime(parseInt(_startHour) + 1, 0, _endHour, _endMinute) != -1) {
                    _everydayTimeSpace =
                        PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                } else {
                    _everydayTimeSpace = PrefixInteger(_startHour, 2) + ':' + PrefixInteger(_startMinute, 2) + ' ~ ' + PrefixInteger(parseInt(_startHour) + 1, 2) + ':00';
                    _timeItem['id'] = '1';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));
                }
            }

            while (compareTime(_hour, _minute, _endHour, _endMinute) == -1) {
                var _nextHour;

                var _nextMinute;

                if (_minute == 0) {
                    _nextHour = _hour;
                    _nextMinute = 30;
                } else {
                    _nextHour = _hour + 1;
                    _nextMinute = 0;
                }

                if (compareTime(_nextHour, _nextMinute, _endHour, _endMinute) != 1) {
                    _everydayTimeSpace = PrefixInteger(_hour, 2) + ':' + PrefixInteger(_minute, 2) + ' ~ ' + PrefixInteger(_nextHour, 2) + ':' + PrefixInteger(_nextMinute, 2);

                    var _id = parseInt(_timeItem['id']) + 1;

                    _timeItem['id'] = _id + '';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _hour = _nextHour;
                    _minute = _nextMinute;
                } else {
                    _everydayTimeSpace = PrefixInteger(_hour, 2) + ':' + PrefixInteger(_minute, 2) + ' ~ ' + PrefixInteger(_endHour, 2) + ':' + PrefixInteger(_endMinute, 2);

                    var _id = parseInt(_timeItem['id']) + 1;

                    _timeItem['id'] = _id + '';
                    _timeItem['value'] = _everydayTimeSpace;

                    _everydayTimeSpaceArray.push(JSON.parse(JSON.stringify(_timeItem)));

                    _hour = _nextHour;
                    _minute = _nextMinute;
                }
            }
        }

        var _days = _customTime.days;

        for (var _j = 0; _j < _days.length; _j++) {
            var _timeItem = {};
            var _day = _days[_j];
            _timeItem['day'] = _day;
            _timeItem['time'] = _everydayTimeSpaceArray;

            _timeArray.push(_timeItem);
        }
    }

    return _timeArray;
} //生成渲染时间控件的数据，返回一个二维数组

function generateCustomTimeDataRough(_startTime, _endTime, _timeArray) {
    var _timeData = [];
    var _timeDataItem = {};
    _timeDataItem['id'] = '0';

    while (_startTime <= _endTime) {
        var _timeItemArray = getTimeData(getWeek(_startTime), _timeArray);

        if (undefined == _timeItemArray) {
            _startTime.setDate(_startTime.getDate() + 1);
        } else {
            var _dayShow = _startTime.Format('MM-dd');

            if (dateDiff(_startTime.Format('yyyy-MM-dd'), new Date().Format('yyyy-MM-dd')) == 0) {
                _dayShow += '(今天)';
            } else if (dateDiff(_startTime.Format('yyyy-MM-dd'), new Date().Format('yyyy-MM-dd')) == 1) {
                _dayShow += '(明天)';
            } else {
                _dayShow += '(' + getWeek(_startTime) + ')';
            }

            if (dateDiff(_startTime.Format('yyyy-MM-dd'), new Date().Format('yyyy-MM-dd')) == 0) {
                var _timeItemArrayLess = []; //可以开始提货的时间在下单当天，判断从当天哪个时间段开始可以自提

                var _currentHour = _startTime.getHours();

                var _currentMinute = _startTime.getMinutes();

                var _timeAvailable = false;

                for (var _i = 0; _i < _timeItemArray.length; _i++) {
                    var _timeItem = _timeItemArray[_i];

                    if (_timeAvailable) {
                        _timeItemArrayLess.push(JSON.parse(JSON.stringify(_timeItem)));
                    } else {
                        //将06:00 ~ 06:30拆分
                        var _temp = _timeItem.value.split('~');

                        var _startHour = _temp[0].trim().split(':')[0];

                        var _startMinute = _temp[0].trim().split(':')[1];

                        var _endHour = _temp[1].trim().split(':')[0];

                        var _endMinute = _temp[1].trim().split(':')[1];

                        if (compareTime(_currentHour, _currentMinute, _startHour, _startMinute) != -1 && compareTime(_currentHour, _currentMinute, _endHour, _endMinute) == -1) {
                            _timeAvailable = true;

                            _timeItemArrayLess.push(JSON.parse(JSON.stringify(_timeItem)));
                        }
                    }
                }

                if (_timeItemArrayLess.length != 0) {
                    var _id = parseInt(_timeDataItem['id']) + 1;

                    _timeDataItem['id'] = _id + '';
                    _timeDataItem['value'] = _dayShow;
                    _timeDataItem['childs'] = _timeItemArrayLess;
                    _timeDataItem['date'] = _startTime.Format('yyyy-MM-dd');

                    _timeData.push(JSON.parse(JSON.stringify(_timeDataItem)));
                }

                _startTime.setDate(_startTime.getDate() + 1);
            } else {
                var _id = parseInt(_timeDataItem['id']) + 1;

                _timeDataItem['id'] = _id + '';
                _timeDataItem['value'] = _dayShow;
                _timeDataItem['childs'] = JSON.parse(JSON.stringify(_timeItemArray));
                _timeDataItem['date'] = _startTime.Format('yyyy-MM-dd');

                _timeData.push(JSON.parse(JSON.stringify(_timeDataItem)));

                _startTime.setDate(_startTime.getDate() + 1);
            }
        }
    }

    return _timeData;
} //生成渲染时间控件的数据，返回一个三维数组

function generateCustomTimeDataDetail(_timeData) {
    var _timeDataDetail = [];
    var _timeDataDetailItem = {};
    _timeDataDetailItem['id'] = '0';

    var _currentDate = new Date();

    var _childs = [];

    for (var _i = 0; _i < _timeData.length; _i++) {
        var _timeDataItem = _timeData[_i];

        var _date = new Date(_timeDataItem.date.replace(/-/g, '/'));

        if (_i == 0) {
            _currentDate = _date;

            _childs.push(JSON.parse(JSON.stringify(_timeDataItem)));
        } else if (_currentDate.getMonth() == _date.getMonth()) {
            _childs.push(JSON.parse(JSON.stringify(_timeDataItem)));
        } else {
            var _id = parseInt(_timeDataDetailItem['id']) + 1;

            _timeDataDetailItem['id'] = _id + '';
            _timeDataDetailItem['value'] = _currentDate.Format('yyyy年MM月');
            _timeDataDetailItem['childs'] = JSON.parse(JSON.stringify(_childs));

            _timeDataDetail.push(JSON.parse(JSON.stringify(_timeDataDetailItem)));

            _childs = [];

            _childs.push(JSON.parse(JSON.stringify(_timeDataItem)));

            _currentDate = _date;
        }
    }

    if (_childs.length != 0) {
        var _id = parseInt(_timeDataDetailItem['id']) + 1;

        _timeDataDetailItem['id'] = _id + '';
        _timeDataDetailItem['value'] = _currentDate.Format('yyyy年MM月');
        _timeDataDetailItem['childs'] = JSON.parse(JSON.stringify(_childs));

        _timeDataDetail.push(JSON.parse(JSON.stringify(_timeDataDetailItem)));
    }

    return _timeDataDetail;
}

function getWeek(_date) {
    if (_date.getDay() == 0) return '周日';
    if (_date.getDay() == 1) return '周一';
    if (_date.getDay() == 2) return '周二';
    if (_date.getDay() == 3) return '周三';
    if (_date.getDay() == 4) return '周四';
    if (_date.getDay() == 5) return '周五';
    if (_date.getDay() == 6) return '周六';
}

function getTimeData(_week, _timeArray) {
    for (var _i = 0; _i < _timeArray.length; _i++) {
        var _timeItem = _timeArray[_i];

        if (_week == _timeItem.day) {
            return _timeItem.time;
        }
    }
} //时间_hour:_minute1和_hour2:_minute2比较
// 相等返回0
// _hour:_minute1小于_hour2:_minute2返回-1
//_hour:_minute1大于_hour2:_minute2返回1

function compareTime(_hour1, _minute1, _hour2, _minute2) {
    if (parseInt(_hour1) < parseInt(_hour2)) {
        return -1;
    } else if (parseInt(_hour1) > parseInt(_hour2)) {
        return 1;
    } else if (parseInt(_minute1) < parseInt(_minute2)) {
        return -1;
    } else if (parseInt(_minute1) > parseInt(_minute2)) {
        return 1;
    } else {
        return 0;
    }
} //输出指定位数的数字，不足的补零
//如num=3,length=3,输出003；num=3,length=2,输出03

function PrefixInteger(num, length) {
    return (Array(length).join('0') + num).slice(-length);
}

function dateDiff(sDate1, sDate2) {
    //sDate1和sDate2是2002-12-18格式
    var aDate, oDate1, oDate2, iDays;
    aDate = sDate1.split('-');
    oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]); //转换为12-18-2002格式

    aDate = sDate2.split('-');
    oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);
    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24); //把相差的毫秒数转换为天数

    return iDays;
}
/*格式化时间*/

Date.prototype.Format = function (fmt) {
    var o = {
        'M+': this.getMonth() + 1,
        //月份
        'd+': this.getDate(),
        //日
        'H+': this.getHours(),
        //小时
        'm+': this.getMinutes(),
        //分
        's+': this.getSeconds(),
        //秒
        'q+': Math.floor((this.getMonth() + 3) / 3),
        //季度
        S: this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));

    for (var k in o) if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));

    return fmt;
};

module.exports = {
    initCustomTimePicker: initCustomTimePicker,
    generateCustomTimeDataDetail: generateCustomTimeDataDetail
};
